package com.hkbigdata.datagovernance.governance.assess.calc;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hkbigdata.datagovernance.governance.assess.Assessor;
import com.hkbigdata.datagovernance.governance.bean.AssessParam;
import com.hkbigdata.datagovernance.governance.bean.GovernanceAssessDetail;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component("NO_ACCESS")
//长期未被访问表
public class NoAccessAssessor extends Assessor {
    @Override
    public void assess(GovernanceAssessDetail governanceAssessDetail, AssessParam assessParam) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//获取考评时间
        String assessDateStr = assessParam.getAssessDate();
//将String日期转为Date类型
        Date assessDate = sdf.parse(assessDateStr);
//获取最后一次访问的时间
        Date tableLastAccessTime = assessParam.getTableMetaInfo().getTableLastAccessTime();
//将日期格式化为天并重新转为Date类型
        String tableLastAccessTimeStr = sdf.format(tableLastAccessTime);
        Date tableLastAccessTimeWithDay = sdf.parse(tableLastAccessTimeStr);

//获得差值
        long diffDays = (assessDate.getTime() - tableLastAccessTimeWithDay.getTime()) / (24 * 60 * 60 * 1000);

//获得参数中的阈值
        String metricParamsJson = assessParam.getGovernanceMetric().getMetricParamsJson();
        JSONObject jsonObject = JSON.parseObject(metricParamsJson);
        Integer days = jsonObject.getInteger("days");

//判断给分
        if (diffDays >= days) {
            governanceAssessDetail.setAssessScore(BigDecimal.ZERO);
            governanceAssessDetail.setAssessProblem("长期未被访问，距离上次访问：" + diffDays + "天");
        }
    }
}

